<template>
  <div class="goodsinfo-container">
    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotuList" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" id="hhh"></div>
    </transition>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            销售价:
            <span class="now_price">¥{{ goodsInfo.sell_price }}</span>&nbsp;&nbsp;市场价:
            <del>¥{{ goodsInfo.market_price }}</del>
          </p>
          <p>
            购买数量:&nbsp;&nbsp;&nbsp;&nbsp;
            <numbox @getCount="getSelectedCount" :max="goodsInfo.stock_quantity"></numbox>
          </p>

          <p class="buy">
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 商品介绍评论区 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号:{{ goodsInfo.goods_no}}</p>
          <p>库存情况:{{ goodsInfo.stock_quantity }}</p>
          <p>上架时间:{{ goodsInfo.add_time | dataFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>


<script>
import MintUI from "mint-ui";
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/numbox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id, //挂载到data上方便后期调用
      lunbotuList: [],
      goodsInfo: {},
      ballFlag: false, //控制小球显示和隐藏的
      selectedCount: 0 //保存用户选中的商品数量.默认认为用户买0个
    };
  },
  created() {
    this.getLunbotu();
    this.getInfo();
  },
  methods: {
    getLunbotu() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          // 赋值之前先循环轮播图数组的每一项,为item添加img属性,因为轮播图组件中,只认识item.img
          result.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbotuList = result.body.message;
        } else {
          // 失败的
          Toast("加载轮播图失败");
        }
      });
    },
    getInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsInfo = result.body.message[0];
        } else {
          Toast("获取数据失败!");
        }
      });
    },
    goDesc(id) {
      // 点击使用编程式导航跳转到图文介绍页面
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    goComment(id) {
      // 点击使用编程式导航跳转到图文介绍页面
      this.$router.push({ name: "goodsComment", params: { id } });
    },
    addToShopCar() {
      // 添加到购物车
      this.ballFlag = !this.ballFlag;
      // { id:商品的ID. count:要购买的数量, price:商品的单价, selected: false}
      // 拼接处一个要保存到store中car数组里的商品对象
      var goodsInfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsInfo.sell_price,
        selected: true
      };
      // 调用commit添加数据
      this.$store.commit("addToCar", goodsInfo);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      // 获取小球在页面中的位置
      var ballPosition = document.getElementById("hhh").getBoundingClientRect();
      // 获取徽标在页面中的位置
      var badgePosition = document
        .getElementById("lwl")
        .getBoundingClientRect();

      var xDist = badgePosition.left - ballPosition.left;
      var yDist = badgePosition.top - ballPosition.top;

      el.offsetWidth;
      el.style.transform = `translate(${xDist}px,${yDist}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      // 当子组件把选中的数量传递给父组件的时候,把选中的值保存到data身上
      this.selectedCount = count;
      console.log("父组件拿到的值为:" + this.selectedCount);
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>


<style lang="scss" scoped>
// v-enter,
// v-leave-to{
//   opacity: 0;
//   transform: translateX(80px);
// }
// v-enter-active,
// v-leave-active{
//   transition: all 0.5s ease;
// }

.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;

  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }

  /deep/.mui-numbox {
    height: 25px;
  }
  .buy {
    margin-top: 15px;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 10px 0;
    }
  }

  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 385px;
    left: 157px;
  }
}
</style>